<template>
  <div class="nominate-page">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1>提名动漫</h1>
      <p>搜索并提名你喜欢的动漫，让它进入社区排行榜</p>
    </div>
    
    <!-- 搜索区域 -->
    <div class="search-section">
      <DoubanAnimeSearch
        @nominate="handleNominate"
        @show-detail="showAnimeDetail"
        @favorite="handleFavorite"
      />
    </div>
    
    <!-- 动漫详情弹窗 -->
    <AnimeDetailModal
      v-if="showDetailModal && selectedAnime"
      :anime="selectedAnime"
      @close="closeDetailModal"
      @vote="handleVote"
      @favorite="handleFavorite"
    />
    
    <!-- 提名功能已简化，移除我的提名列表 -->
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import AnimeDetailModal from '../components/AnimeDetailModal.vue'
import DoubanAnimeSearch from '../components/DoubanAnimeSearch.vue'
import type { Anime } from '../types/anime'
import { nominateAnime as apiNominateAnime, getDoubanAnimeDetail } from '../services/animeService'
import { voteForAnime } from '../services/voteService'
import toastService from '../services/toastService'

// 页面状态
const selectedAnime = ref<Anime | null>(null)
const showDetailModal = ref(false)

// 处理提名
const handleNominate = async (anime: Anime) => {
  if (!anime) return
  
  try {
    // 显示加载状态的toast
    toastService.info('正在获取动漫详情并提名...')
    
    let animeWithDetails = anime
    
    // 如果有豆瓣ID，获取详细信息
    if (anime.doubanId) {
      try {
        const detailedAnime = await getDoubanAnimeDetail(anime.doubanId)
        if (detailedAnime) {
          // 合并原动漫对象和详细信息
          animeWithDetails = {
            ...anime,
            ...detailedAnime
          }
        }
      } catch (detailError) {
        console.error('获取豆瓣详情失败，但继续使用搜索结果中的基本信息:', detailError)
      }
    }
    
    // 调用实际的API进行提名，传递完整的动漫对象
    const success = await apiNominateAnime(animeWithDetails)
    
    if (success) {
      // 提示用户提名成功
      toastService.success(`已成功提名《${animeWithDetails.title}》！`)
    }
  } catch (error) {
    console.error('提名失败:', error)
    toastService.error('提名失败，请稍后再试')
  }
}

// 显示动漫详情
const showAnimeDetail = async (anime: Anime) => {
  try {
    // 显示加载状态
    toastService.info('正在获取动漫详情...')
    
    let animeWithDetails = anime
    
    // 如果有豆瓣ID，获取详细信息
    if (anime.doubanId) {
      try {
        const detailedAnime = await getDoubanAnimeDetail(anime.doubanId)
        if (detailedAnime) {
          // 合并原动漫对象和详细信息
          animeWithDetails = {
            ...anime,
            ...detailedAnime
          }
        }
      } catch (detailError) {
        console.error('获取豆瓣详情失败，但继续使用搜索结果中的基本信息:', detailError)
      }
    }
    
    selectedAnime.value = animeWithDetails
    showDetailModal.value = true
  } finally {
    // 隐藏加载状态
    // toastService.clear() method doesn't exist, removed
  }
}

// 关闭动漫详情弹窗
const closeDetailModal = () => {
  showDetailModal.value = false
  selectedAnime.value = null
}

// 处理投票
const handleVote = async (animeId: number) => {
  try {
    console.log('投票动漫:', animeId)
    // 调用投票API
    await voteForAnime(animeId)
    
    // 更新选中动漫的投票数
    if (selectedAnime.value && selectedAnime.value.id === animeId) {
      selectedAnime.value.votes = (selectedAnime.value.votes || 0) + 1
    }
    
    toastService.success('投票成功！')
  } catch (error) {
    console.error('投票失败:', error)
    toastService.error('投票失败，请稍后再试')
  }
}

// 处理收藏
const handleFavorite = (eventData: number | { animeId: number, isFavorited: boolean }) => {
  // 从事件数据中提取animeId
  const animeId = typeof eventData === 'object' && eventData !== null ? eventData.animeId : Number(eventData);
  
  console.log('收藏动漫:', animeId)
  // 收藏逻辑已在AnimeDetailModal中处理
}

// 移除了提名列表相关功能

// 移除了getStatusText函数，因为不再需要状态显示

// 路由实例
const route = useRoute()

// 组件挂载时的初始化处理
onMounted(() => {
  // 移除了加载提名记录的逻辑
})

// 监听路由变化，确保用户状态正确
watch(() => route.path, () => {
  // 检查登录状态，确保提名功能可用
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  if (!isLoggedIn) {
    // 虽然路由守卫应该已经处理了这个情况，但还是添加一层保障
    window.location.href = '/login'
  }
})
</script>

<style scoped>
    /* 覆盖StandardAnimeGrid的样式，设置为一行三个 */
    :deep(.anime-grid) {
      grid-template-columns: repeat(3, 1fr);
      gap: 20px;
    }
    
    @media (max-width: 992px) {
      :deep(.anime-grid) {
        grid-template-columns: repeat(2, 1fr);
      }
    }
    
    @media (max-width: 768px) {
      :deep(.anime-grid) {
        grid-template-columns: repeat(2, 1fr);
      }
    }
    
    /* 页面容器 */
    .nominate-page {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 16px;
  box-sizing: border-box;
}

/* 页面标题 */
.page-header {
  text-align: center;
  margin-bottom: 48px;
  animation: fadeInUp 0.6s ease-out;
}

.page-header h1 {
  font-size: 2.25rem;
  font-weight: 700;
  color: #1C1C1E;
  margin-bottom: 12px;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.page-header p {
  font-size: 1rem;
  color: #6C6C70;
  max-width: 600px;
  margin: 0 auto;
}

/* 搜索区域 */
.search-section {
  margin-bottom: 48px;
  animation: fadeInUp 0.6s ease-out 0.1s both;
}

.search-input-container {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  max-width: 700px;
  margin-left: auto;
  margin-right: auto;
}

.search-input {
  flex: 1;
  padding: 14px 16px;
  border: 1px solid #E5E5EA;
  border-radius: 12px;
  font-size: 1rem;
  background-color: #F9F9F9;
  color: #1C1C1E;
  transition: all 0.2s ease;
}

.search-input:focus {
  outline: none;
  border-color: #007AFF;
  background-color: white;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.2);
}

.search-input::placeholder {
  color: #8E8E93;
}

.search-button {
  padding: 14px 24px;
  background-color: #007AFF;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(0, 122, 255, 0.2);
  white-space: nowrap;
}

.search-button:hover:not(:disabled) {
  background-color: #0062CC;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 122, 255, 0.3);
}

.search-button:active:not(:disabled) {
  transform: translateY(0);
}

.search-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.search-tips {
  background-color: #F2F2F7;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 0.875rem;
  color: #6C6C70;
  max-width: 700px;
  margin: 0 auto;
}

/* 搜索结果区域 */
.search-results {
  margin-bottom: 60px;
  animation: fadeInUp 0.6s ease-out 0.2s both;
}

.search-results h2 {
  font-size: 1.5rem;
  font-weight: 700;
  color: #1C1C1E;
  margin-bottom: 24px;
  letter-spacing: -0.01em;
}

.loading, .no-results {
  text-align: center;
  padding: 80px 0;
  color: #6C6C70;
  font-size: 1rem;
}

/* 动漫网格 */
.anime-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

/* 移除了提名列表相关的样式 */

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .anime-grid {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 16px;
  }
}

@media (max-width: 768px) {
  .nominate-page {
    padding: 32px 16px;
  }
  
  .page-header h1 {
    font-size: 1.75rem;
  }
  
  .search-input-container {
    flex-direction: column;
  }
  
  .anime-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 16px;
  }
  
  /* 响应式调整已优化 */
  }
  
  .nomination-image {
    width: 100px;
    height: 140px;
  }

@media (max-width: 480px) {
  .nominate-page {
    padding: 24px 12px;
  }
  
  .page-header {
    margin-bottom: 32px;
  }
  
  .page-header h1 {
    font-size: 1.5rem;
  }
  
  .search-section {
    margin-bottom: 32px;
  }
  
  .search-results {
    margin-bottom: 40px;
  }
  
  .anime-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
  }
  
  .nominations-section {
    margin-top: 40px;
  }
}

/* 高对比度模式支持 */
@media (prefers-contrast: high) {
  .page-header p,
  .search-tips,
  .loading,
  .no-results,
  .nomination-date {
    color: #000000;
  }
  
  .search-input {
    border: 2px solid #000000;
    background-color: #ffffff;
  }
  
  .search-input:focus {
    border-color: #0000ff;
  }
  
  .search-tips {
    background-color: #ffffff;
    border: 2px solid #000000;
  }
  
  .search-button {
    border: 2px solid #000000;
  }
  
  .nomination-item {
    border: 2px solid #000000;
  }
  
  /* 移除了深色模式下的状态相关样式，因为不再需要状态显示 */
}

/* 减少动画偏好支持 */
@media (prefers-reduced-motion: reduce) {
  .page-header,
  .search-section,
  .search-results,
  .nominations-section,
  .nomination-item:hover {
    animation: none;
    transform: none;
  }
  
  .search-input:focus,
  .search-button:hover:not(:disabled),
  .search-button:active:not(:disabled) {
    transform: none;
  }
}
</style>